#coding=utf-8
from excelReader import *
from export import *
from utils import *
import os
import importlib
from pathlib import Path
import sys

importlib.reload(sys)

sourcePath = sys.argv[1]
outputPath = sys.argv[2]

def getAllFiles(rootName):
	fileList = []
	for root, dirs, files in os.walk(rootName):
		for filename in files:
			extension = Path(filename).suffix
			if extension == ".xlsx" or extension == ".xls":
				fileList.append(root + "/" + filename)
		for dirname in dirs:
			fileList += getAllFiles(dirname)

	return fileList


if __name__ == '__main__' :
	fileList = getAllFiles(str(sourcePath))
	for filename in fileList:
		filePath = str(filename)
		if "~$" in filePath:
			continue
		print(filePath)
		reader = ExcelReader(filePath)
		listSheet = reader.ReadOutputSheets()
		exporter = ExporterFactory.Create(ExportType.Json)
		for i in range(0, len(listSheet)):
			sheet = listSheet[i]
			value = exporter.Export(sheet)
			path = outputPath + "/" + sheet._path
			Utils.MakeDirs(path)
			file = open(path, mode='w+', encoding='utf-8')
			file.write(str(value))
			file.close()

	# filePath = str("单词配置.xlsx")
	# reader = ExcelReader(filePath)
	# listSheet = reader.ReadOutputSheets()
	# exporter = ExporterFactory.Create(ExportType.Lua)
	# for i in range(0, len(listSheet)):
	# 	sheet = listSheet[i]
	# 	value = exporter.Export(sheet)
	# 	path = outputPath + "/" + sheet._path
	# 	Utils.MakeDirs(path)
	# 	file = open(path, mode='w+', encoding='utf-8')
	# 	file.write(str(value))
	# 	file.close()